<?php

class pages_lib {
 	
	private $ci;
	
 	public function __construct()
 	{
 		log_message('debug', 'Pages library initialized.');
		
		$this->ci =& get_instance();
		
		$this->ci->load->model('db/views/pages_extended', 'vw_pages_extended');
 	}
	
	
	
	public function get_page_menu($iPageParentId, $iMenuId, $iLevel = 0, $iMaxLevel = 1)
	{
	
		// model db/views/page_menus should already have been loaded

		$this->ci->vw_page_menus->page_parent_id = $iPageParentId;
		$this->ci->vw_page_menus->menu_id = $iMenuId;
		$this->ci->vw_page_menus->page_data_menu_type = 'A';
		$this->ci->vw_page_menus->page_status = 'LI';
			
		$aOptions = array();
		$aOptions['order_by'] = array(
								'page_pos' => 'ASC'
		);
		
		
		
		$qPages = $this->ci->vw_page_menus->get($aOptions);
		$iPages = $qPages->num_rows();
		
		if ($iMenuId === 2)
		{
		//	echo $this->ci->db->last_query().'<hr />';
		}
		
		$aPages = array();
		
		for ($i = 0; $i < $iPages; $i++)
		{
			$aPages[$i]['page'] = $qPages->row($i);
			
			$iLevelNext = $iLevel + 1;
			
			if (($iLevelNext <= $iMaxLevel) && ($iPageParentId !== $qPages->row($i)->page_id))
			{
				$aSubPages = $this->get_page_menu($qPages->row($i)->page_id, $iMenuId, $iLevelNext, $iMaxLevel);
				
				if (count($aSubPages) > 0)
				{
					$aPages[$i]['sub_pages'] = $aSubPages;	
				}
			};
		}
		
		
		return $aPages;
	}
	
	
	
	public function build_page_tree($aPages, $iLevel = 0) 
	{
		$iPages = count($aPages);
		
		$sContent = '';
		
		for ($i = 0; $i	< $iPages; $i++)
		{
			$sChildContent = '';	
			
			if (array_key_exists('sub_pages', $aPages[$i]) === true)
			{
				$sChildContent = $this->build_page_tree($aPages[$i]['sub_pages'], ($iLevel + 1));	
			}
			
			$iNextPageId = 0;
			
			if ($i < ($iPages - 1)) 
			{
				$iNextPageId = $aPages[$i+1]['page']->page_id;	
			}
			
			$aParams = array(
						'oPage' => $aPages[$i]['page'], 
						'iNextPageId' => $iNextPageId,
						'sChildContent' => $sChildContent,
						'iLevel' => $iLevel, 
						'i' => $i, 
						'iPages' => $iPages
			);
			
			$sContent .= $this->ci->load->view('platform/'.$this->ci->sPlatform.'/top_menu_li', $aParams, true);
		}
		
		return $sContent;
	}
	
	
	
	public function build_page_menu($aPages, $iLevel = 0) 
	{
		$iPages = count($aPages);
		
		$sContent = '';
		
		for ($i = 0; $i	< $iPages; $i++)
		{
			$sChildContent = '';	
			
			if (array_key_exists('sub_pages', $aPages[$i]) === true)
			{
				$sChildContent = $this->build_page_menu($aPages[$i]['sub_pages'], ($iLevel + 1));	
			}
			
			$iNextPageId = 0;
			
			if ($i < ($iPages - 1)) 
			{
				$iNextPageId = $aPages[$i+1]['page']->page_id;	
			}
			
			$aParams = array(
						'oPage' => $aPages[$i]['page'], 
						'iNextPageId' => $iNextPageId,
						'sChildContent' => $sChildContent,
						'iLevel' => $iLevel, 
						'i' => $i, 
						'iPages' => $iPages
			);
			
			$sContent .= $this->ci->load->view('platform/'.$this->ci->sPlatform.'/left_menu_li', $aParams, true);
		}
		
		return $sContent;
	}
	
	
	
	public function page_details($iPageDataId)
	{
		$this->ci->load->model('db/languages', 'languages');
		
		$qLanguages = $this->ci->languages->get();
		
		
		$this->ci->load->model('db/views/admin/page_details', 'vw_page_details');
		
		$this->ci->vw_page_details->page_data_id = $iPageDataId;
		
		$qPageDetails = $this->ci->vw_page_details->get(1);
		
		
		if ($qPageDetails->num_rows() === 0)
		{
			$oPageDetails = $this->ci->vw_page_details->get_object();
		}
		else 
		{
			$oPageDetails = $qPageDetails->row(0);	
		}
		
		
		$aPageLink = array(
					'page_id' => null,
					'page_url' => ''
		);
		
		if (in_array($oPageDetails->ext_url, array(null, '')) === false)
		{
			if (is_numeric($oPageDetails->ext_url) === false) // it's an external url
			{
				$aPageLink['page_id'] = '';
				$aPageLink['page_url'] = $oPageDetails->ext_url;
			}
			else  // it's an internal link
			{
				$this->ci->load->model('db/views/pages_extended', 'vw_pages_extended');
				
				$this->ci->vw_pages_extended->page_id = $oPageDetails->ext_url;
				$this->ci->vw_pages_extended->page_status = 'LI';
				
				$qPageLink = $this->ci->vw_pages_extended->get(1);
				
				if ($qPageLink->num_rows() === 1)
				{
					$aPageLink['page_id'] = $qPageLink->row(0)->page_id;
					$aPageLink['page_url'] = $qPageLink->row(0)->page_menu_title.' (/'.$qPageLink->row(0)->lang_accr.'/'.$qPageLink->row(0)->page_name.')';	
				}
				
			}
		}
		
	
		$this->ci->load->model('db/sites', 'sites');
		
		if ($this->ci->oSecGroup->site_id !== null)
		{
			$this->ci->sites->site_id = $this->ci->oSecGroup->site_id;
		}
		
		$aOptions = array(
					'order_by' => array('site_name' => 'ASC')
		);
		
		
		
		$qSites = $this->ci->sites->get($aOptions);
		$iSites = $qSites->num_rows();
		
		$sContent = '';
		
		if ($iSites > 0)
		{
			$this->ci->load->model('db/views/pages_extended', 'vw_pages_extended');	
			
			
			for ($i = 0; $i < $iSites; $i++)
			{
				$aPages = $this->ci->vw_pages_extended->get_page_tree(array(
																		'page_name' => null,
																		'site_id' => $qSites->row($i)->site_id
				));	
				
				$sContent .= $this->ci->vw_pages_extended->build_page_tree($aPages, $qSites->row($i), $oPageDetails->page_parent_id, 'page_parent_id');
			}
			
			
			$data = array(
					'sPageTree' => $sContent,
					'sUlClass' => 'page_struct'
			);
			
			$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/build_ul_wrapper', $data, true);
			
			
		}
		
		// now get the right hand navigation
		
				
		$sJs = '';
		
		if ($sContent !== '')
		{
			$sJs = "
				$(document).ready(
					function() {
						$('.".$data['sUlClass']."').treeview({
							persist: 'location',
							collapsed: true,
							unique: true
						});
						
						
						jquery_treeview_iterate('page_parent_id', 'page_tree_node');
						
					}
				);
			";
			
			
			$this->ci->aData['sContent'] = $sContent;
			$this->ci->aData['qLanguages'] = $qLanguages;
			$this->ci->aData['oPageDetails'] = $oPageDetails;
			$this->ci->aData['aPageLink'] = $aPageLink;
			
			$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_details', $this->ci->aData, true);
			
		}
		
		return array(
				'sJs' => $sJs,
				'sContent' => $sContent
		);
		
	}
	
	
	
	public function page_content($iPageDataId)
	{
		$this->ci->load->model('db/page_content', 'page_content');
		
		$this->ci->page_content->page_data_id = $iPageDataId;
		
		$qPageContent = $this->ci->page_content->get(1);
		
		if ($qPageContent->num_rows() === 0)
		{
			$oPageContent = $this->ci->page_content->get_object();	
		}
		else 
		{
			$oPageContent = $qPageContent->row(0);	
		}
	
		$this->ci->aData['oPageContent'] = $oPageContent;
	
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_content', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	
	public function page_navigation($iPageDataId)
	{
		$this->ci->load->model('db/menus', 'menus');
		
		$this->ci->menus->menu_active = 'Y';
		
		$qMenus = $this->ci->menus->get();
		$iMenus = $qMenus->num_rows();
		
		$this->ci->load->model('db/page_data_menus', 'page_data_menus');
		
		for ($i = 0; $i < $iMenus; $i++)
		{
			$this->ci->page_data_menus->clear();
			$this->ci->page_data_menus->page_data_id = $iPageDataId;
			$this->ci->page_data_menus->menu_id = $qMenus->row($i)->menu_id;
			$this->ci->page_data_menus->page_data_menu_type = 'H';
			
			$qMenuHas = $this->ci->page_data_menus->get(array(), 1);
			
			$qMenus->row($i)->menu_has = ($qMenuHas->num_rows() === 1);	


			$this->ci->page_data_menus->clear();
			$this->ci->page_data_menus->page_data_id = $iPageDataId;
			$this->ci->page_data_menus->menu_id = $qMenus->row($i)->menu_id;
			$this->ci->page_data_menus->page_data_menu_type = 'A';
			
			$qMenuAppears = $this->ci->page_data_menus->get(array(), 1);
			
			$qMenus->row($i)->menu_appears = ($qMenuAppears->num_rows() === 1);	

		}
		
		$this->ci->aData['qMenus'] = $qMenus;
		
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_navigation', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	
	public function page_banners($iPageDataId)
	{
		$this->ci->load->model('db/views/banners_by_site', 'vw_banners_by_site');
		
		if ($this->ci->oSecGroup->site_id !== null)
		{
			$this->ci->vw_banners_by_site->site_id = $this->ci->oSecGroup->site_id;
		}
		
		$this->ci->vw_banners_by_site->version_active = 'Y';
		
		$qBanners = $this->ci->vw_banners_by_site->get();
		
		
		$this->ci->load->model('db/page_data_banners', 'page_data_banners');
		
		$this->ci->page_data_banners->page_data_id = $iPageDataId;

		$qPageBanner = $this->ci->page_data_banners->get(array(), 1);
		
		$iPageBannerId = false;	

		if ($qPageBanner->num_rows() === 1)
		{
			$iPageBannerId = $qPageBanner->row(0)->banner_id;	
		}
		
		$this->ci->aData['qBanners'] = $qBanners;
		$this->ci->aData['iPageBannerId'] = $iPageBannerId;
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_banners', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	public function page_spotlight($iPageDataId)
	{
		$this->ci->load->model('db/views/spotlights_by_site', 'vw_spotlights_by_site');
		
		if ($this->ci->oSecGroup->site_id !== null)
		{
			$this->ci->vw_spotlights_by_site->site_id = $this->ci->oSecGroup->site_id;
		}
		
		$this->ci->vw_spotlights_by_site->version_active = 'Y';
		
		$qSpotlights = $this->ci->vw_spotlights_by_site->get();
		
		
		$this->ci->load->model('db/page_data_spotlights', 'page_data_spotlights');
		
		$this->ci->page_data_spotlights->page_data_id = $iPageDataId;

		$qPageSpotlight = $this->ci->page_data_spotlights->get(array(), 1);
		
		$iPageSpotlightId = false;	

		if ($qPageSpotlight->num_rows() === 1)
		{
			$iPageSpotlightId = $qPageSpotlight->row(0)->spotlight_id;	
		}
		
		
		$this->ci->aData['qSpotlights'] = $qSpotlights;
		$this->ci->aData['iPageSpotlightId'] = $iPageSpotlightId;
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_spotlights', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	
	public function page_news($iPageDataId) 
	{
		$sContent = $this->get_page_news($iPageDataId);
		
		
		$sJs = "
			$(document).ready(function() {
				$('#page_option_page_news').bind('click', function(){
					if(!$(this).data('clicked')) {
						$(this).data('clicked', true)
						
						$.post('/admin/pages_ajax/get_page_news', {iPageDataId : ".$iPageDataId."}, function(data){
							if(data.sContent) {
								var sCurrentHtml = $('#page_news_items').html();
								$('#page_news_items').html(sCurrentHtml + data.sContent);
							}
						}, 'json');
					}
				});	
			});
		";
		
		return array('sJs' => $sJs, 'sContent' => $sContent);
	}
	
	
	public function get_page_news($iPageDataId, $iOffset = 0, $bIsAjax = false)
	{
		$aPageNews = array();
		
		if($iPageDataId > 0)
		{
			$this->ci->load->model('db/page_data_news', 'page_data_news');
			
			$this->ci->page_data_news->page_data_id = $iPageDataId;
			
			$qCurrentPageNews = $this->ci->page_data_news->get();
			$iCurrentPageNews = $qCurrentPageNews->num_rows();
	
			for ($i = 0; $i < $iCurrentPageNews; $i++)
			{
				$aPageNews[$i] = $qCurrentPageNews->row($i)->page_id; 	
			}
		}
		
		$this->ci->aData['aPageNews'] = $aPageNews;
		
		$this->ci->load->model('db/views/pages_extended_content', 'vw_pages_extended_content');
		
		if ($this->ci->config->item('admin_pages_news_only') === true)
		{
			$this->ci->vw_pages_extended_content->page_news = 'Y';
		}
		
		
		$this->ci->vw_pages_extended_content->page_status = 'LI';
		$this->ci->vw_pages_extended_content->parent_page_status = 'LI';
		
		if ($this->ci->oSecGroup->site_id !== null)
		{
			$this->ci->vw_pages_extended_content->site_id = $this->ci->oSecGroup->site_id;
		}
		
		$iRemaining = 1;
		
		if($bIsAjax)
		{
			$aOptions = array();
			if(count($aPageNews) > 0)
			{
				$aOptions[] = array('where_not_in' => array('page_id', $aPageNews));
			}
			$aOptions[] = array('limit' => array('100', $iOffset));
			
			$qPageNews = $this->ci->vw_pages_extended_content->get($aOptions);
			
			$iOffset = $iOffset + 100;
			
			$aOptions = array();
			$aOptions[] = array('limit' => array('100', $iOffset));
			
			$qRemaining = $this->ci->vw_pages_extended_content->get($aOptions);
			$iRemaining = $qRemaining->num_rows();
		}
		else
		{
			$aOptions = array();
			if(count($aPageNews) > 0)
			{
				$aOptions[] = array('where_in' => array('page_id', $aPageNews));
			}
			else
			{
				$aOptions[] = array('where' => array('page_id', 0));
			}
			
			$qPageNews = $this->ci->vw_pages_extended_content->get($aOptions);
		}
		
		$this->ci->aData['qPageNews'] = $qPageNews;
		
		$this->ci->aData['iRemaining'] = $iRemaining;
		$this->ci->aData['iPageDataId'] = $iPageDataId;
		$this->ci->aData['iOffset'] = $iOffset;
		$this->ci->aData['bIsAjax'] = $bIsAjax;
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_news', $this->ci->aData, true);
		
		return $sContent;
	}
	
	
	public function page_collaboration($iPageDataId)
	{
		$this->ci->load->model('db/views/user_security_group', 'vw_user_security_group');
		
		$aOptions = array();
		$aOptions[] = array('where' => array('user_id <>', $this->ci->user->userId));
		$aOptions[] = array('order_by' => array('user_lname', 'ASC'));
		$aOptions[] = array('order_by' => array('user_fname', 'ASC'));
		
		$this->ci->vw_user_security_group->clear();
		
		$qCollabUsers = $this->ci->vw_user_security_group->get($aOptions);
		
		$this->ci->aData['qCollabUsers'] = $qCollabUsers;
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_collaboration', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	
	public function page_seo($iPageDataId)
	{
		$this->ci->load->model('db/page_data', 'page_data');
		$this->ci->load->model('db/pages_tbl', 'pages');
		
		$this->ci->page_data->page_data_id = $iPageDataId;
		$qPageData = $this->ci->page_data->get(array(), 1);
		
		
		$oPage = false;
		
		if ($qPageData->num_rows() === 1)
		{
			$this->ci->pages->page_id = $qPageData->row(0)->page_id;
			$qPage = $this->ci->pages->get(array(), 1);
		
			if ($qPage->num_rows() === 1)
			{
				$oPage = $qPage->row(0);
			}
		}
		
		
		if ($oPage === false)
		{
			$oPage = $this->ci->pages->get_object();	
		}
		
		$this->ci->aData['oPage'] = $oPage;
		
		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_seo', $this->ci->aData, true);
		
		return array('sContent' => $sContent);
	}
	
	
	public function page_save_preview($iPageDataId)
	{
		$this->ci->load->model('db/page_data', 'page_data');
		
		$this->ci->page_data->page_data_id = $iPageDataId;
		$qPageData = $this->ci->page_data->get(array(), 1);	
		
		$oPageData = false;
		
		if ($qPageData->num_rows() === 1) 
		{
			$oPageData = $qPageData->row(0);	
		}
		else 
		{
			$oPageData = $this->ci->page_data->get_object();
		}	
		
		$this->ci->aData['oPageData'] = $oPageData;
		
		$sJs = "
			$(document).ready(
				function() {
					$('#page_publish_from').datepicker();
					$('#page_publish_to').datepicker();
				}
			);
		";

		$sContent = $this->ci->load->view('platform/'.$this->ci->sPlatform.'/admin/pages/page_save_preview', $this->ci->aData, true);
		
		return array('sJs' => $sJs, 'sContent' => $sContent);
	}
	
	
	public function get_page_child_tree($iPageParentId, $aOptions = array(), $aChildOptions = array(), $bReturnPageContent = false)
	{
		//db/views/pages_tree & db/views/page_content should already have been clled
		
		$this->ci->pages_tree->page_parent_id = $iPageParentId;
		
		$qChildPages = $this->ci->pages_tree->get($aOptions);
		
		$iChildPages = $qChildPages->num_rows();
		
		for($i = 0; $i < $iChildPages; $i++)
		{
			$qChildPages->row($i)->childPages = $this->get_page_child_tree($qChildPages->row($i)->page_id, $aChildOptions, $aChildOptions, $bReturnPageContent);
			
			if($bReturnPageContent === true)
			{
				$this->ci->page_content->page_id = $qChildPages->row($i)->page_id;
				$this->ci->page_content->page_status = 'LI';
				
				$qPageContent = $this->ci->page_content->get(array(), 1);
				
				$sPageContent = '';
				
				if($qPageContent->num_rows() === 1)
				{
					$sPageContent = $qPageContent->row(0)->page_content;
				}
				
				$qChildPages->row($i)->pageContent = $sPageContent;
			}
		}
		
		return $qChildPages;
	}
	
	public function get_page_hierarchy($aOptions = array(), $aPersistentOptions = array(), $bGroup = true)
	{
		$this->ci->vw_pages_extended->clear();
		
		$aOptions = array_merge($aOptions, $aPersistentOptions);
		
		$aOptions = array('where' => $aOptions);
		
		if($bGroup)
		{
			$aOptions[] = array('group_by' => array('page_id'));
		}
		
		$aOptions[] = array('order_by' => array('page_pos', 'ASC'));
		
		$qPages = $this->ci->vw_pages_extended->get($aOptions);
		
		if($qPages->num_rows() > 0)
		{
			for($i = 0; $i < $qPages->num_rows(); $i++)
			{
				$qPages->row($i)->qChildPages = $this->get_page_hierarchy(array('page_parent_id' => $qPages->row($i)->page_id, 'page_id !=' => $qPages->row($i)->page_id), $aPersistentOptions);
			}
		}
		
		return $qPages;
	}
	
	public function build_page_hierarchy($qPages, $sContainerElement = 'ul', $sContainerElementClass = 'page_container', $sListElement = 'li', $sListElementClass = 'page', $bIncAnchor = false, $sAnchorHref = '', $sAnchorField = NULL)
	{
		$sPages = '';
		
		if($qPages->num_rows() > 0)
		{
			$sPages .= '<'.$sContainerElement.' class="'.$sContainerElementClass.'">';
			
			for($i = 0; $i < $qPages->num_rows(); $i++)
			{
				$sPages .= '<'.$sListElement.' class="'.$sListElementClass.'" id="'.$sListElementClass.'_'.$qPages->row($i)->page_id.'">';
				
				if($bIncAnchor)
				{
					$sHref = $sAnchorHref != '' ? $sAnchorHref : '#';
					$sHref .= ($sAnchorField != false && isset($qPages->row($i)->{$sAnchorField})) ? $qPages->row($i)->{$sAnchorField} : '';
					$sPages .= '<a href="'.$sHref.'"';
					$sPages .= $sHref === '#' ? ' onclick="return false;"' : '';
					$sPages .= '>';
				}
				else
				{
					$sPages .= '<div>';
				}
				
				$sPages .= $qPages->row($i)->page_menu_title;
				
				if($bIncAnchor)
				{
					$sPages .= '</a>';
				}
				else
				{
					$sPages .= '</div>';
				}
				
				if(isset($qPages->row($i)->qChildPages) && $qPages->row($i)->qChildPages->num_rows() > 0)
				{
					$sPages .= $this->build_page_hierarchy($qPages->row($i)->qChildPages, $sContainerElement, $sContainerElementClass, $sListElement, $sListElementClass, $bIncAnchor, $sAnchorHref, $sAnchorField);
				}
				
				$sPages .= '</'.$sListElement.'>';
			}
			
			$sPages .= '</'.$sContainerElement.'>';
		}
		
		return $sPages;
	}
}

?>